package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CashAdvanceSuccessBean;


/**
 * Created by jack on 2017/6/3
 */

public class ItemCashAdvanceSuccessUIComponent extends RelativeLayout {
    public ItemCashAdvanceSuccessUIComponent(Context context) {
        super(context);
    }

    public ItemCashAdvanceSuccessUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemCashAdvanceSuccessUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvTitle;
    private TextView tvContent;
    private View viewLine;
    private EditText etEdit;
    private ImageView ivAvatar;
    private LinearLayout llContent;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvTitle = (TextView) findViewById(R.id.ics_tv_name);
        tvContent = (TextView) findViewById(R.id.ics_tv_content);
        viewLine = findViewById(R.id.ics_view_line);
        etEdit = (EditText) findViewById(R.id.ics_et_edittext);
        ivAvatar = (ImageView) findViewById(R.id.ics_iv_avatar);
        llContent = (LinearLayout) findViewById(R.id.ics_ll_content);
    }

    public void setContent(CashAdvanceSuccessBean.CashListBean data) {
        llContent.setGravity(Gravity.RIGHT);
        tvTitle.setText(FRString.valueOf(data.getName()));
        tvContent.setText(FRString.valueOf(data.getContent()));
        tvContent.setMaxLines(2);
    }

    public void setContent(String title, String content) {
        FRDisplayMetrics.setMargins(viewLine, FRDisplayMetrics.dp2px(getContext(), 15), 0, FRDisplayMetrics.dp2px(getContext(), 15), 0);
        tvTitle.setText(FRString.valueOf(title));
        tvContent.setText(FRString.valueOf(content));
    }

    public void setContentNum(String content) {
        setContent();
        llContent.setGravity(Gravity.RIGHT);
        if (!FRString.isEmpty(content) && !content.equals("0")) {
            tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
            tvContent.setText(FRCollection.appendAsString(content, "张券可用", ""));
        } else {
            tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.cb0b0b0));
            tvContent.setText("无可用");
        }
    }

    private void setContent() {
        tvTitle.setText("现金券");
        viewLine.setVisibility(View.GONE);
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_cash_coupon, 0, 0, 0);
        tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_arrow_gray, 0);
        tvContent.setTextSize(13);
        tvContent.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    }

    public void setContentMoney(String content) {
        setContent();
        if (!FRString.isEmpty(content)) {
            tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
            tvContent.setText(FRCollection.appendAsString("返", content, "元", ""));
        } else {
            tvContent.setTextColor(ContextCompat.getColor(getContext(), R.color.cb0b0b0));
            tvContent.setText("无可用");
        }
    }

    public void setCreditBank(String content) {
        tvContent.setText(content);
    }

    public void setRepaymentDateContent(String content) {
        tvContent.setText(content);
    }

    public void setImageAvatar(String imageUrl) {
        ivAvatar.setVisibility(View.VISIBLE);
        FRImageLoad.loadImage(imageUrl, ivAvatar, R.drawable.icon_no_distinguish_bank);
    }

    public void setCreditRemindBankContent(String title, String content) {
        llContent.setGravity(Gravity.RIGHT);
        tvTitle.setText(FRString.valueOf(title));
        if (FRString.isEmpty(content)) {
            tvContent.setText("请选择所属银行");
        } else {
            tvContent.setText(content);
        }
        tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_arrow_gray, 0);
    }

    public void setCreditRemindCardNumberContent(String title, String content) {
        setCreditRemindWeight(content);
        llContent.setGravity(Gravity.RIGHT);
        tvTitle.setText(title);
    }

    public String getEditContent() {
        return etEdit.getText().toString().trim();
    }

    private void setCreditRemindWeight(String content) {
        etEdit.setVisibility(View.VISIBLE);
        String hint = "请输入信用卡尾号";
        SpannableString ss = new SpannableString(hint);
        ss.setSpan(new AbsoluteSizeSpan(13, true), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        etEdit.setHint(ss);
        if (!FRString.isEmpty(content)) {
            etEdit.setText(content);
            etEdit.setSelection(content.length());
        }
    }
}